<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <form class="layui-form" action="" lay-filter="formExample">

            <div class="layui-form-item" hidden>
                <label class="layui-form-label">id</label>
                <div class="layui-input-inline">
                    <input type="text" name="id" id="id"  autocomplete="off" class="layui-input">
                </div>
                <!-- <div class="layui-form-mid layui-word-aux"><span style="color:red;">保存时验证重复</span></div> -->
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red;">*</span>账号</label>
                <div class="layui-input-inline">
                    <!-- <input type="text" name="account" id="account" lay-verify="required|account"  autocomplete="off" class="layui-input"> -->
                    <input type="text" name="account" id="account" autocomplete="off" class="layui-input">
                </div>
                <!-- <div class="layui-form-mid layui-word-aux"><span style="color:red;">保存时验证重复</span></div> -->
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-inline">
                    <!-- <input type="text" name="user_name"  autocomplete="off" class="layui-input" lay-verify="user_name"> -->
                    <input type="text" name="user_name"  autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux"></div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">管理员</label>
                <div class="layui-input-inline">
                    <select name="user_type">
                        <option value="" selected="">请选择状态</option>
                        <option value="0">是</option>
                        <option value="1">否</option>
                    </select>
                </div>

                <div class="layui-form-mid layui-word-aux"></div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">电话</label>
                <div class="layui-input-inline">
                    <!-- <input type="text" name="phonenumber" autocomplete="off" class="layui-input"> -->
                    <input type="text" name="phonenumber" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux"></div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">邮箱</label>
                <div class="layui-input-inline">
                    <!-- <input type="text" name="email" autocomplete="off" class="layui-input" placeholder="非密邮箱" lay-verify="allowEmptyEmail"> -->
                    <input type="text" name="email" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux"></div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">部门</label>
                <div class="layui-input-inline">
                    <input type="text" name="department" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux"></div>
            </div>

            <!-- <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red;">*</span>部门</label>
                <div class="layui-input-inline">
                    <input type="text" name="showDepartment" id="department_addUser" lay-verify="required" readonly  autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux"></div>
            </div>

            <div class="layui-form-item" hidden>
                <label class="layui-form-label">部门ID</label>
                <div class="layui-input-inline">
                    <input type="text" name="departmentid" id="departmentid_addUser" lay-verify="required" readonly  autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux"></div>
            </div> -->


          


            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="demo1">确定</button>
                </div>
            </div>
        </form>



    </div>
</div>
<script>
    layui.use(['form', 'layedit', 'laydate','tree'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , tree = layui.tree
            , laydate = layui.laydate;

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        //自定义验证规则
        form.verify({
            // title: function (value) {
            //     if (value.length < 5) {
            //         return '标题至少得5个字符啊';
            //     }
            // }
            // , pass: [
            //     /^[\S]{6,12}$/
            //     , '密码必须6到12位，且不能出现空格'
            // ]
            // , content: function (value) {
            //     layedit.sync(editIndex);
            // }
            
            account: function (value) {
                var re = /^[a-zA-Z]{4,9}$/;
                var re = /^\w{4,10}$/;
                debugger

                if(!re.test(value)){
                    return '账号格式错误';
                }
            }
            ,user_name: function (value) {
                if (value.length > 10) {
                    return '姓名最大长度为10';
                }
            }
            ,allowEmptyEmail: function (value) {
                if(value!=""){
                    if(!/^[a-z0-9._%-]+@([a-z0-9-]+.)+[a-z]{2,4}$|^1[3|4|5|7|8]d{9}$/.test(value)){
                        return'邮箱格式错误';
                    }
                }
                
            }
        });



     

        //表单初始赋值
        /*data_userMgr是从userMgr页面传递过来的*/
        if (data_userMgr) {
            //id输入框不可用
            $("#id").attr("disabled","disabled");
            $("#account").attr("disabled","disabled");

            //赋值
            form.val('formExample', {
            "id": data_userMgr.id // "name": "value"
            
            , "account": data_userMgr.account
            , "user_name": data_userMgr.user_name
             ,"user_type": data_userMgr.user_type
            , "phonenumber": data_userMgr.phonenumber
            , "email": data_userMgr.email
            , "department": data_userMgr.department
            // , "showDepartment": data_userMgr.showDepartment
            // , "departmentid": data_userMgr.departmentid


            // , "interest": 1
            // , "like[write]": true //复选框选中状态
            // , "close": true //开关状态
            // , "sex": "女"
            // , "desc": "我爱 layui"
        })
        }
        
   //监听提交
   form.on('submit(demo1)', function (data) {
    console.log("data")
            console.log(data)
            // if (data_userMgr){
            //     data.field["type"] = "edit"
            // }else{
            //     data.field["type"] = "add"
            // }
            url = ""
            // debugger
            if(data_userMgr){
                url = "/admin/user/admin-edit"
            }else{
                url = "/admin/user/admin-add"
            }
            shared.ajaxPost(url, data.field, function (res) {
                if (res) {
                    layer.msg(res, {
                        icon: 1,
                    time: 1000 //2秒关闭（如果不配置，默认是3秒）
                    }, function(){
                        layer.closeAll();
                    });
                }
            })
        return false;
    });
    


        $("#department_addUser").on("click", function () {
        var content = layui.miniPage.getHrefContent('mypages/usual/userMgt/addUser_chooseDepartment.html');
        var openWH = layui.miniPage.getOpenWidthHeight();
        //不能用 var 前缀
        opreatType = "add";
        var index = layer.open({
            title: '选择部门',
            type: 1,
            shade: 0.2,
            // shade: [0.8, '#393D49'],//底层遮罩样式
            maxmin: true,
            shadeClose: true,
            offset: '100px',
            // area : [ 'auto', '448px' ],
            area : [ '500px', '500px' ],
            // area: [openWH[0] / 2 + 'px', openWH[1] / 2 - 30 + 'px'],
            // offset: [openWH[2] + 'px', openWH[3] + 'px'],
            content: content,
            end: function () {
                // tempList = []
                // var treeObj = $.fn.zTree.getZTreeObj("tree");
                // var nodes = treeObj.getNodes();
                //  rec(nodes);
                // console.log(tempList)
            }
        });
        $(window).on("resize", function () {
            layer.full(index);
        });
        return false;
    });

       



    })

        
   
 

  
</script>